import router from '@/router'
import store from './store'
import NProgress from 'nprogress' // 引入一份进读条插件
import 'nprogress/nprogress.css' // 引入进度条样式

const whiteList = ['/login', '/404']

router.beforeEach(async(to, from, next) => {
  NProgress.start() // 开启进度条
  const token = store.getters.token
  if (token) {
    if (!store.getters.id) {
      await store.dispatch('user/getUserInfo')
    }
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
  NProgress.done()
})
// 后置守卫
router.afterEach((to, from, next) => {
  if (to.meta.title) {
    document.title = '人力资源管理系统-' + to.meta.title
  } else {
    document.title = '人力资源管理系统'
  }
  NProgress.done() // 结束进度条
})
